package com.mobile.challenge.web;

import com.mobile.challenge.core.Result;
import com.mobile.challenge.core.ResultGenerator;
import com.mobile.challenge.dao.ChallengeInfoMapper;
import com.mobile.challenge.model.*;
import com.mobile.challenge.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lix on 2018/06/04.
 */
@Api(value = "挑战接口",tags = {"挑战记录管理"})
@RestController
@RequestMapping("/challenge")
public class ChallengeController {

    @Resource
    private ChallengeInfoService challengeInfoService;
    @Resource
    private ChallengeInfoMapper tChallengeInfoMapper;
    /**
     * 页面初始化信息获取接口
     * @param phoneNo
     * @return
     */
//    @RequestMapping(value = "/Initialization", method = RequestMethod.POST, produces = {"application/json"})
//    @ResponseStatus(HttpStatus.OK)
//    public Result initialization(@ApiParam(value = "加密手机号", required = true)
//                               @RequestParam("phoneNo") String phoneNo){
//          Map data = new HashMap();
//        Map data = prizeRecordService.findByPhoneNo(phoneNo,times);
//        //当月剩余多少个奖
//        int remainingCount = prizeService.findById(1).getPrizeTotal();
//        //当日剩余挑战次数
//        int challengeCount = 0;
//        //是否报过名 1是，0否
//        int ifTakePartIn = 0;
//        /*
//            本用户已达到中奖上限
//         */
//        if((int)data.get("code") == 0){
//            //当日剩余挑战0次
//            challengeCount = 0;
//            //参加过活动
//            ifTakePartIn = 1;
//        }
//        else{
//            /*
//                获取是否报过名 1是，0否
//             */
//            ifTakePartIn = userService.findByPhoneNo(phoneNo);
//            /*
//                获取当日剩余挑战次数
//             */
//            Map challengeCountMap = challengeInfoService.findByPhoneNoToday(phoneNo,timesOneDay);
//            challengeCount = (int) challengeCountMap.get("count");
//        }
//        data.put("ifTakePartIn",ifTakePartIn);
//        data.put("prizesCount",remainingCount);
//        data.put("challengeCount",challengeCount);
//
//        return ResultGenerator.genSuccessResult(data);
//    }


    /**
     * 获取当日剩余挑战次数
     * @param user
     * @return
     */
    @RequestMapping(value = "/remaining/times/today", method = RequestMethod.POST, produces = {"application/json"})
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取当日剩余挑战次数", notes = "必须提供一个phoneNo")
    public Result getRemCount4Challenge(@RequestBody User user
                                             ) {
        Map data = new HashMap();
        if(user.getPhoneNo().equals("")){
            return ResultGenerator.genFailResult("手机号为空",data);
        }
        data = challengeInfoService.findByPhoneNoToday(user.getPhoneNo());

        return ResultGenerator.genSuccessResult(data);
    }

    /**
     * 开始挑战接口(加锁)
     * @param user
     * @return
     */
    @RequestMapping(value = "/start", method = RequestMethod.POST, produces = {"application/json"})
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "开始挑战", notes = "必须提供一个phoneNo,点击'开始计时'触发此接口")
    public synchronized Result startChallenge(@RequestBody  User user) {
        Map data = new HashMap();
        if(user.getPhoneNo().equals("")){
            return ResultGenerator.genFailResult("手机号为空",data);
        }
        ChallengeInfo challengeInfo = new ChallengeInfo();
        challengeInfo.setPhoneNo(user.getPhoneNo());
        Map rs = challengeInfoService.start(challengeInfo);

        data.put("id",tChallengeInfoMapper.queryLastByPhoneNoToday(user.getPhoneNo()).getId());
        if(!(boolean)rs.get("code")){
            return ResultGenerator.genFailResult((String) rs.get("msg"),data);
        }
        return ResultGenerator.genSuccessResult(data);
    }

    /**
     * 结束挑战接口(加锁)
     * @param challengeInfo
     * @return
     */
    @RequestMapping(value = "/end", method = RequestMethod.POST, produces = {"application/json"})
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "结束挑战", notes = "必须提供一个phoneNo,点击'结束计时'触发此接口")
    public synchronized Result endChallenge(@RequestBody  ChallengeInfo challengeInfo) {
        Map data = new HashMap();
        //结束记时
        Map rs = challengeInfoService.end(challengeInfo);
        data.put("count",rs.get("count"));
        data.put("noGet",rs.get("noGet"));

        if(!(boolean)rs.get("code")){
            return ResultGenerator.genFailResult((String) rs.get("msg"),data);
        }
        return ResultGenerator.genSuccessResult(data);
    }

}